package cn.mars4j.nutch.web.parser;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.webharvest.definition.ScraperConfiguration;
import org.webharvest.runtime.Scraper;

import cn.mars4j.nutch.web.bean.Metadata;

import com.thoughtworks.xstream.XStream;

/**
 * 基础解析器
 * BaseParser.java
 *
 * @author sqd
 * 2014年5月8日
 */
public abstract class BaseParser implements Parser {
	
	/**
	 * 解析用配置文件
	 */
	private String config;
	
	/**
	 * 配置文件参数
	 */
	protected Map<Object, Object> context = new HashMap<Object, Object>();
	
	/**
	 * 构造函数
	 * @param config 解析用配置文件
	 */
	public BaseParser(String config) {
		this.config = config;
	}
	
	/**
	 * 设置配置文件参数
	 * @param context
	 */
	protected abstract Map<Object, Object> getContext();

	/**
	 * 抽取页面信息
	 * @param mkey 关键词
	 * @throws Exception
	 */
	public List<Metadata> extract() throws Exception {
		
		String classpath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
        ScraperConfiguration config = new ScraperConfiguration(classpath + this.config);
        Scraper scraper = new Scraper(config, null);

        scraper.setDebug(true);

        long startTime = System.currentTimeMillis();
        
        scraper.getContext().putAll(getContext());
        
        scraper.execute();
        String resultStr = (String)scraper.getContext().getVar("resultList").toString();
        XStream xstream = new XStream( ); 
        xstream.alias("Metadata", Metadata.class);
        List<Metadata> resultList = (List<Metadata>)xstream.fromXML(resultStr); 
        //System.out.println(xstream.toXML(resultList));
        System.out.println("花费时间: " + (System.currentTimeMillis() - startTime));
		return (resultList);
	}
}